package tom.spring.cloud.zipkin;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import zipkin.server.ZipkinServer;
import zipkin2.server.internal.EnableZipkinServer;
import zipkin2.server.internal.RegisterZipkinHealthIndicators;

/**
 * zipkin server 链路追踪分析服务，twitter推出。
 * 访问：http://localhost:8080/zipkin/
 *
 * @author ZHUFEIFEI
 */
@EnableZipkinServer
@EnableDiscoveryClient
@SpringBootApplication
public class ZipkinServer01Application {

    //这些参数可以通过命令行传入，此处为了测试直接代码设置
    static {
        //zipkin-server-shared.yml
        System.setProperty("QUERY_PORT", "8080");
        System.setProperty("RABBIT_ADDRESSES", "localhost:5672");
        System.setProperty("RABBIT_USER", "guest");
        System.setProperty("RABBIT_PASSWORD", "guest");

        //存储到mysql ZipkinMySQLStorageProperties
        //创建数据库zipkin
        //初始化mysql，zipkin-storage-mysql.jar/mysql.sql
        System.setProperty("STORAGE_TYPE", "mysql");
        System.setProperty("MYSQL_JDBC_URL", "jdbc:mysql://localhost:3306/zipkin?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&allowMultiQueries=true");
        System.setProperty("MYSQL_USER", "root");
        System.setProperty("MYSQL_PASS", "root");
    }

    /**
     * 该方式启动ZipkinServer，使用zipkin-server-shared.yml配置文件
     * 本项目中的application.yml就无效了，bootstrap.yml还是有效的.
     * 如果不用zipkin-server.yml可以不指定spring.config.name
     * {@link ZipkinServer}
     * @param args
     */
    public static void main(String[] args) {
        //zipkin-server-shared.yml
        new SpringApplicationBuilder(ZipkinServer.class)
                .listeners(new RegisterZipkinHealthIndicators())
                //指定配置文件名称为zipkin-server，默认是application
                //zipkin-server.yml中指定profile=shared
                .properties("spring.config.name=zipkin-server").run(args);
    }
}
